﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class InstrumentBillEditorForm : Form {
        EmptyFeeInstrumentBill CurrentBill { get; set; }
        Action Callback { get; set; }
        public InstrumentBillEditorForm( ) {
            InitializeComponent( );
        }

        public InstrumentBillEditorForm(EmptyFeeInstrumentBill bill,Action action=null) :this(){
            CurrentBill = bill;
            Callback = action;
        }

        private void InstrumentBillEditorForm_Load(object sender, EventArgs e) {
            dateTimePicker1.Value = CurrentBill.LastDate != null && CurrentBill.LastDate != DateTime.MinValue
                                        ? CurrentBill.LastDate.Value
                                        : GlobalSetting.FirstDate;
            textBox1.Text = CurrentBill.LastValue.ToString();
            if (textBox1.Text != "0" && textBox1.Text!= "") {
                textBox1.ReadOnly = true;
            }
            textBox2.Text = CurrentBill.CurrentValue.ToString();
            label6.Text = "" + CurrentBill.UnitPrice.ToString("￥##0.00");
            label7.Text = "" + ((CurrentBill.CurrentValue - CurrentBill.LastValue) * CurrentBill.UnitPrice).ToString("￥##0.00");
        }

        private void button1_Click(object sender, EventArgs e) {
            if (CheckValidate()) {
                CurrentBill.LastDate = dateTimePicker1.Value;
                CurrentBill.LastValue = Helper.GetInt(textBox1.Text.Trim());
                CurrentBill.CurrentValue = Helper.GetInt(textBox2.Text.Trim());
                CurrentBill.CurrentDate = CurrentBill.LastDate.Value.AddMonths(1);
                CurrentBill.CurrentNumber = CurrentBill.CurrentValue - CurrentBill.LastValue;
                CurrentBill.CallableMoney = CurrentBill.UnitPrice*CurrentBill.CurrentNumber;
                if (Callback != null) {
                    Callback();
                }
                this.Close();
            }
            else {
                FormHelper.ShowErrorMsg("编辑仪表账单信息失败，检查输入项");
            }
        }

        private bool CheckValidate() {
            int u = 0;
            return dateTimePicker1.Value != GlobalSetting.FirstDate
                   && !string.IsNullOrEmpty(textBox2.Text.Trim())
                   && int.TryParse(textBox2.Text.Trim(),out u)
                   && !string.IsNullOrEmpty(textBox1.Text.Trim())
                     && int.TryParse(textBox1.Text.Trim( ), out u)
                   && Helper.GetInt(textBox2.Text.Trim()) >= Helper.GetInt(textBox1.Text.Trim());
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close();
        }

       

        private void textBox2_Leave(object sender, EventArgs e) {
            label7.Text = ((Helper.GetInt(textBox2.Text.Trim( )) - Helper.GetInt(textBox1.Text.Trim( ))) * CurrentBill.UnitPrice).ToString("￥##0.00");
        }
    }
}
